package com.seuic.hnapps;

import android.content.Context;
import android.support.multidex.MultiDex;

import com.google.gson.Gson;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.seuic.hnapps.bean.StatisticalData_bean;
import com.seuic.hnapps.bean.Summary_Bean;
import com.seuic.hnapps.util.SpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Application extends android.app.Application {

    /**
     * 每天的流水号都要重置
     * 每天的流水号都要不重复，每次操作都要加一；
     * Application 贯穿整个app
     */
    @Override
    public void onCreate() {
        Logger.addLogAdapter(new AndroidLogAdapter());
        super.onCreate();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        Logger.e("Date获取当前日期时间" + simpleDateFormat.format(date));
        Gson gson = new Gson();

        //获取时间 存当前时间
        String Ztime = SpUtils.getSPUtil(getBaseContext()).getString("ztime", "第一次打开APP");
        //不是第一次打开app 说明存储过当前时间
        if (!Ztime.equals("第一次打开APP")) {
            //如果当期时间和之前存储过的一样，说明是同一天，不重置域11 凭证号
            if (Ztime.equals(simpleDateFormat.format(date))) {
                Logger.e(String.valueOf(SpUtils.getSPUtil(getBaseContext()).getInt("s_Num", 1)));
                Logger.e(String.valueOf(SpUtils.getSPUtil(getBaseContext()).getInt("pz_Num", 1)));
            } else {
                //如果当期时间和之前存储过的不一样，说明不是同一天，重置域11 凭证号
                SpUtils.getSPUtil(getBaseContext()).putString("ztime", simpleDateFormat.format(date));
                SpUtils.getSPUtil(getBaseContext()).putInt("s_Num", 100000);

///凭证号
                SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", 1);

                StatisticalData_bean bean = new StatisticalData_bean("", "", "", "", "", "", "", "");
                List<StatisticalData_bean> beans = new ArrayList<>();
                beans.add(bean);
                //正常交易
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", ""), gson.toJson(beans));
                //汇总查询
                Summary_Bean beanall = new Summary_Bean("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
                List<Summary_Bean> beanalls = new ArrayList<>();
                beanalls.add(beanall);
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));

                //异常交易
                StatisticalData_bean unTBean = new StatisticalData_bean("", "", "", "", "", "", "", "");
                List<StatisticalData_bean> unTBeans = new ArrayList<>();
                unTBeans.add(unTBean);
                SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "UN_DATA", gson.toJson(unTBeans));

            }
        }
        //第一次打开app 存当前时间,存域11 凭证号
        else {
            Logger.e("第一次打开app");
            SpUtils.getSPUtil(getBaseContext()).putString("ztime", simpleDateFormat.format(date));
            SpUtils.getSPUtil(getBaseContext()).putInt("s_Num", 100000);
            SpUtils.getSPUtil(getBaseContext()).putInt("pz_Num", 1);

            StatisticalData_bean bean = new StatisticalData_bean("", "", "", "", "", "", "", "");
            List<StatisticalData_bean> beans = new ArrayList<>();
            beans.add(bean);
            //正常交易
            SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", ""), gson.toJson(beans));
            //汇总查询
            Summary_Bean beanall = new Summary_Bean("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
            List<Summary_Bean> beanalls = new ArrayList<>();
            beanalls.add(beanall);
            SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "ALL_DATA", gson.toJson(beanalls));
            //异常交易
            StatisticalData_bean unTBean = new StatisticalData_bean("", "", "", "", "", "", "", "");
            List<StatisticalData_bean> unTBeans = new ArrayList<>();
            unTBeans.add(unTBean);
            SpUtils.getSPUtil(getApplicationContext()).putString(simpleDateFormat.format(date).replace("/", "") + "UN_DATA", gson.toJson(unTBeans));

        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
